package io.virtualapp.home;

import android.annotation.SuppressLint;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import io.virtualapp.R;
import io.virtualapp.bean.Contract;
import io.virtualapp.bean.MyCardBean;
import io.virtualapp.bean.UpdateBean;
import io.virtualapp.mylibrary.util.ToastUtils;
import zxing.BitmapUtil;
import zxing.activity.AboutActivity;
import zxing.activity.CodeDisplayActivity;
import zxing.activity.MakingRecordsActivity;
import zxing.activity.MyCardActivity;
import zxing.activity.QRCodeUtil;
import zxing.activity.ScanningListActivity;
import zxing.activity.SuggestListActivity;
import zxing.activity.VipPayActivity;

/**
 * Created by cheng
 * PackageName VirtualApp
 * 2018/8/3 14:57
 */

public class CenterFragment extends Fragment {
    @BindView(R.id.head_back)
    ImageView head_back;
    @BindView(R.id.head_center_title)
    TextView head_center_title;
    @BindView(R.id.rela_about)
    RelativeLayout rela_about;
    @BindView(R.id.kefu)
    RelativeLayout kefu;
    @BindView(R.id.lv_feedback)
    RelativeLayout lvFeedback;
    Unbinder unbinder;
    @BindView(R.id.rela_s)
    RelativeLayout rela_s;
    @BindView(R.id.rela_make)
    RelativeLayout rela_make;
    @BindView(R.id.my_card)
    RelativeLayout my_card;
    @BindView(R.id.qr_mycard)
    ImageView qr_mycard;
    private List<MyCardBean> datas;
    @BindView(R.id.my_name)
    TextView my_name;
    @BindView(R.id.lin_c)
    LinearLayout lin_c;
    @BindView(R.id.my_vip)
    RelativeLayout myVip;
    @BindView(R.id.text_en)
    TextView text_en;
    @BindView(R.id.lin_qr)
    LinearLayout lin_qr;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_center, null);
        unbinder = ButterKnife.bind(this, view);
        head_back.setVisibility(View.GONE);
        head_center_title.setText("我的");
        initAction();
        return view;

    }

    protected void initAction() {
        datas = new ArrayList<>();
        datas = DataSupport.findAll(MyCardBean.class);
        if (datas != null && !datas.isEmpty()) {
            lin_qr.setVisibility(View.VISIBLE);
            text_en.setText("快去跟朋友分享名片吧!");
            if (!TextUtils.isEmpty(datas.get(0).getContents())) {
//                Create2QR2(datas.get(0).getContents(), qr_mycard);
                qr_mycard.setImageBitmap(QRCodeUtil.createQRCodeBitmapClors(
                        datas.get(0).getContents(),
                        (px2dip(getContext(), 30)),
                        getResources().getColor(R.color.colo6),
                        getResources().getColor(R.color.white)));
            }
            my_name.setText(datas.get(0).getName());
//            lin_qr.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    Intent intent = new Intent(getActivity(), CodeDisplayActivity.class);
//                    intent.putExtra("data", datas.get(0).getContents());
//                    intent.putExtra("title", "我的名片");
//                    startActivity(intent);
//                }
//            });

        }
        lin_c.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (datas != null && !datas.isEmpty()) {
                    Intent intent = new Intent(getActivity(), CodeDisplayActivity.class);
                    intent.putExtra("data", datas.get(0).getContents());
                    intent.putExtra("title", "我的名片");
                    startActivity(intent);
                } else
                    startActivity(new Intent(getActivity(), MyCardActivity.class));
            }
        });
        //关于我们
        rela_about.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), AboutActivity.class));
            }
        });
        kefu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showistimeexpirseAlerDialog();
            }
        });
        //意见反馈
        lvFeedback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), SuggestListActivity.class));
            }
        });
        rela_s.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), ScanningListActivity.class));
            }
        });
        rela_make.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), MakingRecordsActivity.class));
            }
        });
        my_card.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), MyCardActivity.class));
            }
        });
        myVip.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), VipPayActivity.class));
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        initAction();
    }

    public static int px2dip(Context context, int dp) {
        float density = context.getResources().getDisplayMetrics().density;
        return (int) (dp * density + 0.5);

    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    private UpdateBean bean;
    Contract contractBean;

    private void Create2QR2(String urls, ImageView imageView) {
        String uri = urls;
        int mScreenWidth = 0;
        Bitmap bitmap;
        try {
            DisplayMetrics dm = new DisplayMetrics();
            getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
            mScreenWidth = dm.widthPixels;
            bitmap = BitmapUtil.createQRImage(uri, mScreenWidth,
                    null);//不添加中间图标
//            bitmap = BitmapUtil.createQRImage(uri, mScreenWidth,
//                    BitmapFactory.decodeResource(getResources(), R.mipmap.me));//添加中间图标

            if (bitmap != null) {
                imageView.setImageBitmap(bitmap);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    @SuppressLint("SetTextI18n")
    private void showistimeexpirseAlerDialog() {
        bean = DataSupport.findFirst(UpdateBean.class);
        contractBean = DataSupport.findFirst(Contract.class);
        Log.e("TAG", contractBean.getNum() + "contractBean");

        final AlertDialog.Builder builder = new AlertDialog.Builder(getContext(), R.style.DialogTransparent);
        final ClipboardManager clip = (ClipboardManager) getActivity().getSystemService(Context.CLIPBOARD_SERVICE);
        View view = LayoutInflater.from(getContext()).inflate(R.layout.popup_view, null);
        builder.setView(view);
        final AlertDialog dialog = builder.show();
        final TextView text_lx = view.findViewById(R.id.text_lx);
        if (contractBean != null) {
            text_lx.setText("客服"+contractBean.getTxt() + ":" + contractBean.getNum());
        }

        view.findViewById(R.id.btn_openNow).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ToastUtils.showShortToast("已复制到粘贴板");
                clip.setText(contractBean.getNum());
                dialog.dismiss();
            }
        });


    }


}
